package engine

import (
	"context"
	"time"

	"github.com/robfig/cron/v3"
	"go.etcd.io/etcd/clientv3"
)

// 作业实体
type GarkJob struct {
	Id                     string
	Group                  string
	Description            string
	Name                   string
	ShardingCount          int
	ShardingItemParameters string
	Payload                string
	Enabled                bool
	Cron                   string
	Version                int
	Remark                 string
	StartTime              time.Time
	EndTime                time.Time
	CreateTime             time.Time
	UpdateTime             time.Time
}

// 群组配置
type GroupConf struct {
	Name   string `json:"name"`
	Remark string `json:"remark"`
}

// 作业配置
type GarkJobConf struct {
	Id                     string `json:"id"`
	Name                   string `json:"name"`
	Group                  string `json:"group"`
	ShardingCount          int    `json:"shardingCount"`
	ShardingItemParameters string `json:"shardingItemParameters"`
	Payload                string `json:"payload"`
	Cron                   string `json:"cron"`
	Remark                 string `json:"remark"`
	Version                int    `json:"version"`
	Target                 string `json:"target"`
}

// 作业调度计划, 系统中的调度执行单元
type SchedulePlan struct {
	Id                     string
	Name                   string
	Group                  string
	Cron                   string
	Target                 string
	Payload                string
	Mobile                 string
	Remark                 string
	ShardingCount          int
	ShardingItemParameters string
	Version                int
	Schedule               cron.Schedule
	NextTime               time.Time
	PrevTime               time.Time
}

// 下次执行时间
func (sp *SchedulePlan) Next() {
	sp.PrevTime = sp.NextTime
	sp.NextTime = sp.Schedule.Next(sp.PrevTime)
}

// key变化事件
type KeyChangeEvent struct {
	Type  KeyChangeEventType
	Key   string
	Value []byte
}

type JobChangeEvent struct {
	Type JobChangeEventType
	Conf GarkJobConf
}

// 监听key 变化响应
type WatchKeyChangeResponse struct {
	Event      chan *KeyChangeEvent
	CancelFunc context.CancelFunc
	Watcher    clientv3.Watcher
}

type TxResponse struct {
	Success bool
	LeaseID clientv3.LeaseID
	Lease   clientv3.Lease
	Key     string
	Value   string
}
